package com.jackiechan.hello.day01;

import java.util.Scanner;

/**
 * 要求用户输入一个计算表达式，可以使用加减乘除。
 * 只处理一次运算的，不要有连续加减乘除的表达式,且不做小数计算。(例:1+2+3)
 * 例如:
 * 1+2
 * 然后经过处理计算结果并输出:1+2=3
 * @author Xiloer
 *
 */
public class Test06 {
	public static void main(String[] args) {
		while (true){
			System.out.print("请输入一个一次整数运算式:");
			String input = new Scanner(System.in).nextLine();
//			add(input);
			count(input);
		}

//		System.out.println(parseInt("456132789"));
	}

	private static void count(String input) {
		StringBuilder sb = new StringBuilder(input);
	}

	private static void add(String str) {
		StringBuilder exp = new StringBuilder(str);
		if (str.contains("+")){
			int[] ints = substring(str,"+");
			System.out.println(ints[0]+" + "+ ints[1] +" = "+ (ints[0]+ints[1]));
		}else if (str.contains("-")){
			int[] ints = substring(str,"-");
			System.out.println(ints[0]+" - "+ ints[1] +" = "+ (ints[0]-ints[1]));
		}else if (str.contains("*")){
			int[] ints = substring(str,"*");
			System.out.println(ints[0]+" * "+ ints[1] +" = "+ (ints[0]*ints[1]));
		}else if (str.contains("/")){
			int[] ints = substring(str,"/");
			System.out.println(ints[0]+" / "+ ints[1] +" = "+ (ints[0]/ints[1]));
		}else{
			System.out.println("请输入正确的表达式");
		}
	}
	public static int[] substring(String str, String symbol){
		StringBuilder exp = new StringBuilder(str);
		int index = exp.indexOf(symbol);
		String a = exp.substring(0,index);
		String b = exp.substring(index+1,exp.length());
		Integer sum1 = new Integer(a.trim());
		Integer sum2 = new Integer(b.trim());
		return new int[]{sum1, sum2};
	}
	public static int parseInt(String str){
		//最后要生成的数字
		int num = 0;
		//临时变量，用于计算对应位数的数字
		int flag = 0;
		for(int i=0;i<str.length();i++){
			flag = (str.charAt(i)-48);
			/*
			 * 这里是将对应的数字计算为对应的位，例如百位数字就要用该数字乘以10的2次方
			 * 得到
			 * 可以用Math的相关方法处理(自行查看API文档)
			 */
			for(int n=0;n<str.length()-1-i;n++){
				flag*=10;
			}
			num+=flag;
		}
		return num;
	}
}








